package com.ganbing518.commons.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.security.PublicKey;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

public class SslHttpClient {

    private static final int HTTP_STATUS_OK = 200;

    public static final String DEFAULT_ENCODING = "UTF-8";

    public static String postStringData(String urlLink, Map<String, String> headers, String data) throws Exception {
        String result;

        HttpPost post = new HttpPost(new URI(urlLink));

        if (null != headers) {
            for (String key : headers.keySet()) {
                post.addHeader(key, headers.get(key));
            }
        }
        // 设置请求参数
        post.setEntity(new StringEntity(data, DEFAULT_ENCODING));

        // http调用获取返回数据
        result = getResponseData(SslUtils.getHttpClient(), post);
        return result;
    }

    public static String getStringData(String urlLink, Map<String, String> headers) throws Exception {
        String result;
        HttpGet get = new HttpGet(new URI(urlLink));

        if (null != headers) {
            for (String key : headers.keySet()) {
                get.addHeader(key, headers.get(key));
            }
        }

        result = getResponseData(SslUtils.getHttpClient(), get);

        return result;
    }

    private static String getResponseData(HttpClient client, HttpUriRequest request) throws Exception {

        try {
            // 设置返回数据处理
            ResponseHandler<String> responseHandler = response -> {
                int status = response.getStatusLine().getStatusCode();

                // 只有200的时候才按成功处理
                if (status == HTTP_STATUS_OK) {
                    HttpEntity entity = response.getEntity();
                    // 指定返回信息编码为UTF-8
                    return entity != null ? EntityUtils.toString(entity, DEFAULT_ENCODING) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }

            };

            return client.execute(request, responseHandler);
        } catch (Exception ex) {
            request.abort();
            throw ex;
        }
    }

    /**
     * 解析请求中的参数作为上下
     *
     * @param httpRequestBase
     * @return
     * @throws IOException
     */
    private static String parseContext(HttpUriRequest httpRequestBase) {
        String context = "";
        if (httpRequestBase instanceof HttpGet) {
            context = httpRequestBase.getURI().toString();
        } else if (httpRequestBase instanceof HttpPost) {
            HttpPost httpPost = (HttpPost) httpRequestBase;
            HttpEntity entity = httpPost.getEntity();
            if (entity instanceof StringEntity) {
                try {
                    context = EntityUtils.toString(entity);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return context;

    }

    public static void main(String[] args) throws Exception {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate)cf.generateCertificate(new FileInputStream("C:\\Users\\Administrator\\Desktop\\63709301000017.crt"));


        RSAPublicKey priKey = (RSAPublicKey)cert.getPublicKey();

        String appModules = priKey.getModulus().toString(16).toUpperCase();
        String appExp = priKey.getPublicExponent().toString(16);

        System.out.println(appExp);

        System.out.println(appModules);
    }

}